package top.lvkun.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.PageList;
import top.lvkun.basic.utils.RedisUtils;
import top.lvkun.org.domain.Employee;
import top.lvkun.pet.domain.Pet;
import top.lvkun.pet.domain.PetType;
import top.lvkun.pet.query.PetQuery;
import top.lvkun.pet.service.IPetService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 前台发布寻主信息：下拉列表获取所有宠物类型
     * 后台管理：下拉列表获取所有宠物类型
     * @return
     */
    @GetMapping("/types")
    public List<PetType> getPetTypes(){
        return petService.getPetTypes();
    }


    /**
     * 新增产品及修改产品信息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping
    public AjaxResult AddOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        try {

            System.out.println(pet.getCoatColor());
            //获取当前登录用户
            Employee employee = RedisUtils.INSTANCE.getUser(request, Employee.class);
            //设置当前查看的宠物为后台管理系统当前登录的门店管理员
            pet.setShop_id(employee.getShop().getId());
            pet.setShop(employee.getShop());
            petService.AddOrUpdate(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("提交失败！");
        }

    }

    /**
     * 后台管理：分页和高级查询
     * 当前登录用户只能查看自己门店的宠物
     * @param petQuery
     * @param request
     * @return
     */
    @PostMapping
    public PageList<Pet> pageList(@RequestBody PetQuery petQuery, HttpServletRequest request){
        Employee admin = RedisUtils.INSTANCE.getUser(request, Employee.class);
        petQuery.setShop_id(admin.getShop().getId());
        return petService.queryData(petQuery);
    }

    /**
     * 后台管理：删除宠物
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务繁忙！");
        }
    }

    /**
     * 后台管理：宠物批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/up")
    public AjaxResult petUp(@RequestBody List<Long> ids){
        try {
            return petService.patchUp(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上架失败，"+e.getMessage());
        }
    }

    /**
     * 后台管理：宠物批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/down")
    public AjaxResult petDown(@RequestBody List<Long> ids){
        try {
            return petService.patchDown(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下架失败!"+e.getMessage());
        }
    }
}
